#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Android到鸿蒙字符串资源转换工具使用示例
"""

import os
import sys
from HarmonyResMigrator import ResourceMigrator


def example_basic_usage():
    """基本使用示例"""
    print("=" * 50)
    print("基本使用示例")
    print("=" * 50)
    
    # 创建迁移器实例
    migrator = ResourceMigrator()
    
    # 指定Android资源目录
    android_res_dir = "test_data/android_res"
    
    # 加载Android资源
    print(f"1. 加载Android资源: {android_res_dir}")
    if not migrator.load_all_resources(android_res_dir):
        print("❌ 加载失败")
        return
    
    print("✅ 加载成功")
    print(f"   找到 {len(migrator.resource_map)} 种语言的资源")
    
    # 示例1: 按资源名称查找
    print("\n2. 按资源名称查找示例")
    target_names = ["app_name", "welcome_message"]
    result = migrator.find_by_names(target_names)
    
    print(f"   查找资源: {target_names}")
    print(f"   找到 {len(result)} 种语言的匹配资源")
    
    # 生成鸿蒙资源
    output_dir = "example_output"
    if migrator.generate_harmony_resources(result, output_dir):
        print(f"✅ 鸿蒙资源已生成到: {output_dir}")
    else:
        print("❌ 生成失败")


def example_search_by_value():
    """按值搜索示例"""
    print("\n" + "=" * 50)
    print("按字符串值搜索示例")
    print("=" * 50)
    
    migrator = ResourceMigrator()
    migrator.load_all_resources("test_data/android_res")
    
    # 搜索包含特定文本的资源
    search_values = ["Welcome", "提交"]
    result = migrator.find_by_values(search_values)
    
    print(f"搜索包含以下文本的资源: {search_values}")
    
    for lang, strings in result.items():
        print(f"\n{lang} 语言:")
        for name, value in strings.items():
            print(f"  {name}: {value}")


def example_language_qualified():
    """语言限定搜索示例"""
    print("\n" + "=" * 50)
    print("语言限定搜索示例")
    print("=" * 50)
    
    migrator = ResourceMigrator()
    migrator.load_all_resources("test_data/android_res")
    
    # 使用语言限定标识
    qualified_ids = ["zh_CN:app_name", "en:english_only"]
    result = migrator.find_by_language_qualified(qualified_ids)
    
    print(f"搜索语言限定资源: {qualified_ids}")
    
    for lang, strings in result.items():
        print(f"\n{lang} 语言:")
        for name, value in strings.items():
            print(f"  {name}: {value}")


def example_batch_conversion():
    """批量转换示例"""
    print("\n" + "=" * 50)
    print("批量转换示例")
    print("=" * 50)
    
    migrator = ResourceMigrator()
    migrator.load_all_resources("test_data/android_res")
    
    # 批量转换多个资源
    batch_names = [
        "app_name", 
        "button_submit", 
        "button_cancel",
        "menu_settings",
        "menu_about"
    ]
    
    result = migrator.find_by_names(batch_names)
    
    print(f"批量转换 {len(batch_names)} 个资源:")
    for name in batch_names:
        print(f"  - {name}")
    
    # 统计转换结果
    total_converted = sum(len(strings) for strings in result.values())
    print(f"\n转换结果: {total_converted} 个字符串项")
    
    # 生成到指定目录
    output_dir = "batch_output"
    if migrator.generate_harmony_resources(result, output_dir):
        print(f"✅ 批量转换完成，输出到: {output_dir}")
        
        # 显示生成的文件
        import pathlib
        json_files = list(pathlib.Path(output_dir).rglob("*.json"))
        print(f"   生成了 {len(json_files)} 个JSON文件:")
        for json_file in json_files:
            print(f"     {json_file}")


def main():
    """运行所有示例"""
    print("Android到鸿蒙字符串资源转换工具 - 使用示例")
    print("=" * 60)
    
    # 检查测试数据是否存在
    if not os.path.exists("test_data/android_res"):
        print("❌ 测试数据不存在，请先运行 test_migrator.py 创建测试数据")
        return
    
    try:
        # 运行各种示例
        example_basic_usage()
        example_search_by_value()
        example_language_qualified()
        example_batch_conversion()
        
        print("\n" + "=" * 60)
        print("🎉 所有示例运行完成！")
        print("\n提示:")
        print("- 查看生成的输出目录了解鸿蒙资源格式")
        print("- 使用 'python HarmonyResMigrator.py' 启动GUI界面")
        print("- 使用 'python HarmonyResMigrator.py --help' 查看CLI帮助")
        
    except Exception as e:
        print(f"❌ 运行示例时出错: {e}")
        import traceback
        traceback.print_exc()


if __name__ == "__main__":
    main()